package miaofusdk

import (
	"encoding/json"
	"errors"
	"fmt"
)

type OrgBean struct {
	Id                    int    `json:"id,omitempty,string"`
	Name                  string `json:"name,omitempty"`
	Code                  string `json:"code,omitempty"`
	Address               string `json:"address,omitempty"`
	Creater               string `json:"creater,omitempty"`
	ContactName           string `json:"contact_name,omitempty"`
	ContactNumber         string `json:"contact_number,omitempty"`
	ContactEmail          string `json:"contace_email,omitempty"`
	License               string `json:"license,omitempty"`
	AccountName           string `json:"account_name,omitempty"`
	AccountBank           string `json:"account_bank,omitempty"`
	AccountCard           string `json:"account_card,omitempty"`
	LegalPersonName       string `json:"legal_person_name,omitempty"`
	LegalPersonId         string `json:"legal_person_id,omitempty"`
	LegalPersonIdUrlFront string `json:"legal_person_id_url_front,omitempty"`
	LegalPersonIdUrlBack  string `json:"legal_person_id_url_back,omitempty"`
	Parent                string `json:"parent,omitempty"`
	Level                 string `json:"level,omitempty"`
	UserId                int    `json:"user_id,omitempty,string"`
}

type RespOrg struct {
	Response
	Data OrgBean `json:"data,omitempty"`
}

type RespOrgs struct {
	Response
	Page       int       `json:"page,string"`
	Rows       int       `json:"rows,string"`
	TotalCount int       `json:"total_count,string"`
	TotalPage  int       `json:"total_page,string"`
	List       []OrgBean `json:"list,omitempty,string"`
}

func (s *SrvShopManager) GetOrgById(org_id int) (OrgBean, error) {
	resp := RespOrg{}
	http_request := s.GetPost(s.GetUrl() + "/org/query")
	http_request.Param("org_id", fmt.Sprintf("%d", org_id))
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(resp.Msg)
	} else {
		return resp.Data, nil
	}
}

func (s *SrvShopManager) GetOrgByCode(code string) (OrgBean, error) {
	resp := RespOrg{}
	http_request := s.GetPost(s.GetUrl() + "/org/query")
	http_request.Param("code", code)
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(resp.Msg)
	} else {
		return resp.Data, nil
	}
}
